package zone.haiwen.demo.config;

import zone.haiwen.demo.MyHttpInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;


@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    /**
	 * Description: 让bean提前加载，让拦截器中的@Autowired生效
	 * @author zhangHaiWen
	 * @data 2021/7/7 19:48
	 * @return org.springframework.web.servlet.HandlerInterceptor
	 **/
	@Bean
	public HandlerInterceptor getMyInterceptor() {
		return new MyHttpInterceptor();
	}

	/**
	 * Description
	 * @author zhangHaiWen
	 * @data 2021/7/7 19:49
	 * @return void
	 **/
	@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 解决 swagger-ui.html 404报错
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

        // 解决 doc.html 404 报错
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");

    }

	/**
	 * Description: 可定义多个拦截器
	 * @author zhangHaiWen
	 * @data 2021/7/7 19:49
	 * @return void
	 **/
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// 定义过滤拦截的url名称，拦截所有请求
		registry.addInterceptor(this.getMyInterceptor()).addPathPatterns("/**");
		super.addInterceptors(registry);
	}

}